<template>
    <div style="position: relative;">
        <div style="display: none; height: 0px; width: 0px;">
            <form name="loginForm" method="post" action="/hgweb/loginNew.aspx" id="loginForm" target="loginFrame">
                <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODk2MDQ1OTgwZGQdwuGpVH4gBCfaZXbWss1fnjxt0KKBrt5JeWhpARd14w=="
                />
                <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CD4E5C93" />
                <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKF7agLAqXVsrMJArWptJYLzCY2W1dZzfQ0NVcrNA5KoRUcR/CmLstaDQ/RkfVIjtU="
                />
                <input name="txtUserName" type="text" id="txtUserName" value="hgfb" style="width:107px; height:16px;" />
                <input name="txtPassWord" type="password" id="txtPassWord" value="hgfb" style="width:107px; height:16px;" />
                <input type="image" name="ImageButton1" id="ImageButton1" class="bnt" Text="登 录" style="border-width:0px;" />
            </form>
            <iframe v-show="showLoginIframe" id="loginFrame" name="loginFrame"></iframe>
        </div>
    </div>
</template>

<script>
    import axios from 'axios'

    export default {
        name: 'hgweb',
        data() {
            return {
                showLoginIframe: false,
                loading: false,
                iframeHeight: 1500
            }
        },
        props: {
            iframeUrl: {
                type: String,
                default: ''
            }
        },
        mounted() {
            console.log('初始化')
            this.loginHandler();
        },
        methods: {
            loginIframeOnload() {
                this.showLoginIframe = false;
                console.log('登录成功');
                this.$emit('hgweb-login');
                this.loading = false;
            },
            loginHandler() {
                this.loading = true;
                axios(`/hgweb/${this.iframeUrl}`).then(res => {
                    var data = res.data;
                    if (data.indexOf('images/login/EN_login_03.gif') > -1) {
                        console.log('未登陆');

                        this.showLoginIframe = true;
                        var loginFrame = document.getElementById("loginFrame")
                        if (loginFrame.attachEvent) {
                            loginFrame.attachEvent("onload", this.loginIframeOnload);
                        } else {
                            loginFrame.onload = this.loginIframeOnload;
                        }

                        // 开始登陆
                        var loginForm = this.$refs.loginForm;
                        document.getElementById("loginForm").submit();
                    } else {
                        console.log('已登陆');
                        this.$emit('hgweb-login');
                        this.loading = false;
                    }
                }).catch(e => {
                    console.error(e);
                    this.loading = false;
                })
            }
        },
        watch: {

        }
    }
</script>

<style>
</style>